package com.project.erp.dao.fobcost;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.project.erp.common.dao.HibernateDao;
import com.project.erp.common.utils.Page;
import com.project.erp.model.fobcost.FobCost;

@Repository
public class FobCostDao extends HibernateDao<FobCost, Long> {
	
	/**
	 * 根据条件查询总记录
	 * @param page
	 * @param fobCost
	 * @return
	 */
	public Page<FobCost> getFobCostPageBySearch(Page<FobCost> page,FobCost fobCost){
		if(null == fobCost){
			page = this.getAll(page);
		}
		List<Criterion> list = new ArrayList<Criterion>();
		if(fobCost.getIsTemplate() != null){
			Criterion item = Restrictions.eq("isTemplate",fobCost.getIsTemplate());
			list.add(item);
		}
		if(StringUtils.isNotEmpty(fobCost.getItem())){
			Criterion item = Restrictions.ilike("item",fobCost.getItem(),MatchMode.ANYWHERE);
			list.add(item);
		}
		if(StringUtils.isNotEmpty(fobCost.getFobSize())){
			Criterion fobSize = Restrictions.ilike("fobSize",fobCost.getFobSize(),MatchMode.ANYWHERE);
			list.add(fobSize);
		}
		if(StringUtils.isNotEmpty(fobCost.getCreateUser())){
			Criterion createUser = Restrictions.ilike("createUser",fobCost.getCreateUser(),MatchMode.ANYWHERE);
			list.add(createUser);
		}
		if(fobCost.getStartTime() != null){
			Criterion startDate = Restrictions.ge("createTime", fobCost.getStartTime());
			list.add(startDate);
		}
		
		if(fobCost.getEndTime() != null){
			Criterion endTime = Restrictions.le("createTime",fobCost.getEndTime());
			list.add(endTime);
		}
		
		page = this.findPage(page,list.toArray(new Criterion[list.size()]));
		
		return page;
	}
	
	/**
	 * 根据id列表删除
	 * @param idsList
	 */
	public void delFobCostByfobCostIds(final List<Long> idsList){
		this.deleteByIdsList(idsList);
	}
	
	@SuppressWarnings("unchecked")
	public FobCost getFobCostByItemAndSize(String item,String fobSize){
		Criteria c = this.getSession().createCriteria(FobCost.class);
		c.add(Restrictions.eq("item", item));
		c.add(Restrictions.eq("fobSize", fobSize));
		c.add(Restrictions.eq("isTemplate", true));//模板筛选条件

		List<FobCost> fobCosts = c.list();
		FobCost fobCost = null;
		
		if((fobCosts != null) && (fobCosts.size() > 0)){
			fobCost = fobCosts.get(0);
		}
		return fobCost;
	}
}
